package com.joker.aigc.chat.service.impl;

import com.joker.aigc.chat.domain.dto.PushMessageDTO;
import com.joker.aigc.chat.domain.entity.MQConstant;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketBaseVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketMessageSendVo;
import com.joker.aigc.chat.domain.enums.WebSocketPushTypeEnum;
import com.joker.aigc.chat.domain.enums.WebSocketVoTypeEnum;
import com.joker.aigc.chat.service.PushService;
import com.joker.aigc.utils.AssertUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;


@Service
@Slf4j
@RequiredArgsConstructor
public class PushServiceImpl implements PushService {

    private final RabbitTemplate rabbitTemplate;

    @Override
    public void sendWebSocket(WebSocketBaseVo<?> msg, Long uid) {
        // 单发消息
        AssertUtil.isNotEmpty(msg.getType(), "对不起msg的类型为空");
        String routerKey = WebSocketVoTypeEnum.of(msg.getType()).getRouterKey();
        AssertUtil.isNotEmpty(routerKey, "routerKey为空");
        PushMessageDTO pushMessageDTO = new PushMessageDTO();
        pushMessageDTO.setPushType(WebSocketPushTypeEnum.USER.getType());
        pushMessageDTO.setUidList(Collections.singletonList(uid));
        pushMessageDTO.setWsBaseMsg(msg);
        rabbitTemplate.convertAndSend(MQConstant.MESSAGE_EXANGE, MQConstant.MESSAGE_QUEUE + "." + routerKey, pushMessageDTO);
    }

    @Override
    public void sendWebSocketAll(WebSocketBaseVo<?> msg, Long skipUid) {
        // 全体发送
        AssertUtil.isNotEmpty(msg.getType(), "对不起msg的类型为空");
        String routerKey = WebSocketVoTypeEnum.of(msg.getType()).getRouterKey();
        AssertUtil.isNotEmpty(routerKey, "routerKey为空");
        PushMessageDTO pushMessageDTO = new PushMessageDTO();
        pushMessageDTO.setPushType(WebSocketPushTypeEnum.ALL.getType());
        pushMessageDTO.setWsBaseMsg(msg);
        pushMessageDTO.setUidList(Collections.singletonList(skipUid));
        rabbitTemplate.convertAndSend(MQConstant.MESSAGE_EXANGE, MQConstant.MESSAGE_QUEUE + "." + routerKey, pushMessageDTO);
    }

    @Override
    public void sendWebSocket(WebSocketBaseVo<?> msg, List<Long> uids) {
        // 群发
        AssertUtil.isNotEmpty(msg.getType(), "对不起msg的类型为空");
        String routerKey = WebSocketVoTypeEnum.of(msg.getType()).getRouterKey();
        AssertUtil.isNotEmpty(routerKey, "routerKey为空");
        PushMessageDTO pushMessageDTO = new PushMessageDTO();
        pushMessageDTO.setPushType(WebSocketPushTypeEnum.USER.getType());
        pushMessageDTO.setUidList(uids);
        pushMessageDTO.setWsBaseMsg(msg);
        rabbitTemplate.convertAndSend(MQConstant.MESSAGE_EXANGE, MQConstant.MESSAGE_QUEUE + "." + routerKey, pushMessageDTO);
    }

    @Override
    public void sendMessage(WebSocketBaseVo<WebSocketMessageSendVo> msg) {
        AssertUtil.isNotEmpty(msg.getType(), "对不起msg的类型为空");
        String routerKey = WebSocketVoTypeEnum.of(msg.getType()).getRouterKey();
        AssertUtil.isNotEmpty(routerKey, "routerKey为空");
        rabbitTemplate.convertAndSend(MQConstant.WEBSOCKET_EXCHANHE, routerKey, msg);
        log.info("rabbitmq消息发送成功:{},routerKey:{}", msg, routerKey);
    }

}
